package common;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

public class FileEncodingConvter {

	
	/**
	 * gbkFile ---> utf8File
	 * @author: 1952593542
	 * @Date: 2023年6月15日上午10:57:18
	 * @param srcFileName
	 * @param destFileName
	 * @throws IOException
	 */
	public static void gbkFileToUtf8File(String srcFileName, String destFileName) throws IOException {

		String line_separator = System.getProperty("line.separator");

		FileInputStream fis = new FileInputStream(srcFileName);

		StringBuffer content = new StringBuffer();

		DataInputStream in = new DataInputStream(fis);

		BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));// , "UTF-8"
		String line = null;

		while ((line = d.readLine()) != null) {
			content.append(line + line_separator);			
		}

		d.close();
		
		in.close();

		fis.close();

		Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName), StandardCharsets.UTF_8);

		ow.write(content.toString());

		ow.close();
	}
}
